package com.vstudy.zx.controller;

import com.alibaba.fastjson.JSONObject;
import com.vstudy.zx.entity.Conversation;
import com.vstudy.zx.entity.Teacher;
import com.vstudy.zx.service.ConversationService;
import com.vstudy.zx.utils.page.ConversationPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/conversation")
public class ConversationController {
    @Autowired
    private ConversationService service;

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(ConversationPage page, Model model, HttpServletRequest request) {
        Teacher teacher = (Teacher) request.getSession().getAttribute("teacher");
        page.setTe_id(teacher.getTe_id());
//        page.setStatus(false);
        List<Conversation> conversations = service.getConversations(page);
        model.addAttribute("page", page);
        model.addAttribute("convs", conversations);
        return "zx/conversation/all.jsp";
    }

    @RequestMapping(value = "/unread", method = RequestMethod.GET)
    public String unRead(ConversationPage page, Model model, HttpServletRequest request) {
        Teacher teacher = (Teacher) request.getSession().getAttribute("teacher");
        page.setTe_id(teacher.getTe_id());
        page.setStatus(false);
        List<Conversation> conversations = service.getConversations(page);
        model.addAttribute("page", page);
        model.addAttribute("convs", conversations);
        return "zx/conversation/unRead.jsp";
    }

    @RequestMapping(value = "/readed", method = RequestMethod.GET)
    public String readed(ConversationPage page, Model model, HttpServletRequest request) {
        Teacher teacher = (Teacher) request.getSession().getAttribute("teacher");
        page.setTe_id(teacher.getTe_id());
        page.setStatus(true);
        List<Conversation> conversations = service.getConversations(page);
        model.addAttribute("page", page);
        model.addAttribute("convs", conversations);
        return "zx/conversation/readed.jsp";
    }

    // status: unRead, read, all
    @PostMapping("/count")
    @ResponseBody
    public Object convCount(String status, HttpServletRequest request) {
        Teacher teacher = (Teacher) request.getSession().getAttribute("teacher");
        Boolean s = null;
        switch (status) {
            case "unRead":
                s = false;
                break;
            case "read":
                s = true;
                break;
            default:
                break;
        }
        int count = service.count(teacher.getTe_id(), s);
        return count;
    }

    // 标记已读 {"ids":[1,2,3...]}  -> 1/0
    @RequestMapping(value = "/signRead", method = RequestMethod.POST)
    @ResponseBody
    public Object signRead(@RequestBody JSONObject ids) {
        Object[] arr = ids.getJSONArray("ids").toArray();
        if (arr.length < 1) {
            return 0;
        }
        List<Long> l_ids = new ArrayList<>();
        for (Object o : arr) {
            l_ids.add(Long.parseLong(o.toString()));
        }
        Boolean rs = service.signRead(l_ids);

        return rs ? 1 : 0;
    }

    // 删除会话
    @RequestMapping(value = "/removeConvs", method = RequestMethod.POST)
    @ResponseBody
    public Object removeConvs(@RequestBody JSONObject ids) {
        Object[] arr = ids.getJSONArray("ids").toArray();
        if (arr.length < 1) {
            return 0;
        }
        List<Long> l_ids = new ArrayList<>();
        for (Object o : arr) {
            l_ids.add(Long.parseLong(o.toString()));
        }
        Boolean rs = service.removeConvs(l_ids);
        return "";
    }

    @RequestMapping(value = "/getConId", method = RequestMethod.POST)
    @ResponseBody
    public Object getConId(Long stuId, HttpServletRequest request) {
        Teacher teacher = (Teacher) request.getSession().getAttribute("teacher");
        Long conId = service.getConId(stuId, teacher.getTe_id());
        return conId;
    }

}
